package com.hzw.controller;

import com.hzw.bean.ArticleComment;
import com.hzw.dto.ApiResult;
import com.hzw.dto.ArticleCommentDTO;
import com.hzw.service.ArticleCommentService;
import com.hzw.utils.ObjectUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

import static com.hzw.api.Api.CLIENT_ERROR;
import static com.hzw.api.Api.SERVER_ERROR;

/**
 * Created by Administrator on 2019/4/1.
 */
@Controller
@CrossOrigin
@RequestMapping(value = "/articleComment")
public class ArticleCommentController {
    @Resource
    private ArticleCommentService articleCommentService;

    @RequestMapping(value = "/addComment",method = RequestMethod.POST)
    @ResponseBody
    public ApiResult addComment(@RequestBody ArticleComment articleComment){
        ApiResult result = new ApiResult();
        String articleId = articleComment.getArticleId();
        if(ObjectUtils.isNullOrEmpty(articleId)){
            result.error(CLIENT_ERROR,"articleId不能为空！");
            return result;
        }else{
            try{
                articleCommentService.addComment(articleComment);
                result.setMsg("新增评论成功！");
            }catch (Exception e){
                result.error(SERVER_ERROR,"新增评论失败，原因：" + e.getMessage());
                return result;
            }
        }
        return result;
    }

    @RequestMapping(value = "/deleteCommentByCommentId",method = RequestMethod.POST)
    @ResponseBody
    public ApiResult deleteCommentByCommentId(@RequestBody ArticleComment articleComment){
        ApiResult result = new ApiResult();
        String id = articleComment.getId();
        if(ObjectUtils.isNullOrEmpty(id)){
            result.error(CLIENT_ERROR,"id不能为空！");
            return result;
        }else{
            try{
                int count = articleCommentService.deleteCommentByCommentId(id);
                if(count == 1){
                    result.setMsg("删除评论成功！");
                }else {
                    result.error(SERVER_ERROR,"删除评论失败，不止删除一条或未删除成功" );
                }

            }catch (Exception e){
                result.error(SERVER_ERROR,"删除评论失败，原因：" + e.getMessage());
                return result;
            }
        }
        return result;
    }

    @RequestMapping(value = "/selectCommentByArticleId",method = RequestMethod.POST)
    @ResponseBody
    public ApiResult selectCommentByArticleId(@RequestBody ArticleComment articleComment){
        ApiResult result = new ApiResult();
        String articleId = articleComment.getArticleId();
        if(ObjectUtils.isNullOrEmpty(articleId)){
            result.error(CLIENT_ERROR,"articleId不能为空！");
            return result;
        }else{
            try{
                List<ArticleCommentDTO> articleCommentDTOList = articleCommentService.selectCommentByArticleId(articleId);
                result.setMsg("查找评论成功！");
                result.setData(articleCommentDTOList);
            }catch (Exception e){
                result.error(SERVER_ERROR,"查找评论失败，原因：" + e.getMessage());
                return result;
            }
        }
        return result;
    }
}
